<?php

/*
	Class: Careers_AdminController

	About: Author
		Rich Joslin

	About: License
		<http://communit.as/docs/license>

	About: See Also
		- <Cts_Controller_Action_Admin>
*/
class Careers_AdminController extends Cts_Controller_Action_Admin {

	/* Group: Instance Methods */

	function init() {
		parent::init();
	}

	/* Group: Actions */

	/*
		Function: index
			Displays a list of all careers as well as some bulk actions and filters.

		View Variables:
			$countries - An array of countries to populate the country filter dropdown.
			$career_types - An array of career types to populate the career type filter dropdown.
			$careers - An array of careers to list.
			$q - The search keywords, if submitted.
	*/
	function indexAction() {
		$request = new Cts_Request($this->getRequest());
		$careers_table = new CareersViewCareers();
		$where = null;
		if ($request->has('q')) {
			$where = $careers_table->getWhereClauseByKeywords($request->q);
			$this->view->q = $request->q;
		}
		$params = array();
        $per_page = Cts_Registry::get('careers_per_page');
		$page = $this->_request->getParam('page', 0);
		$total = $careers_table->getCountByWhereClause($where);
		$url = "/careers/admin/index";
		$order = "job_title asc";
		$this->makePager($page, $per_page, $total, $url, $params);
		$this->view->careers = $careers_table->fetchAllArray($where, $order, $per_page, $per_page * $page);
	}

	/*
		Function: edit
	*/
	function editAction() {
		$request = new Cts_Request($this->getRequest());
	   	$errors = array();
		$career_id = 0;
		if (!$request->has('id')) {
       		$this->_redirect("/default/auth/missing");
		} else {
			$careers_table = new CareersCareers();
			$career_id = $request->id;
			if ($career_id == -1) {
				$career = array(
					'id' => "-1",
					'location_id' => null,
					'job_title' => "New Career",
					'department' => "",
					'description' => "",
				);
				$this->view->is_new = true;
			} else {
				$career = $careers_table->findArray($career_id);
			}
			if (count($career) >= 1) {
				$this->view->career = $career;
				$this->view->career_id = $career_id;
				if ($this->getRequest()->isPost()) {
					$request->addValidator("job_title", "Please enter a job title.");
					$request->addValidator("description", "Please enter a description.");
					if (!$request->isValid()) {
						$errors = array_merge($errors, $request->getValidationErrors());
					}
					if (count($errors) <= 0) {
						$career['job_title'] = $request->job_title;
						$career['location_id'] = $request->location_id;
						$career['department'] = $request->department;
						$career['description'] = $request->description;
						$career['last_modified_on'] = date(DB_DATETIME_FORMAT);
						$this->view->career = $career;
						$params = array();
						if ($career_id >= 1) {
							// UPDATE EXISTING
							$params['career'] = $career;
							$params = $this->_cts_plugin->doFilter("careers_admin_edit_pre_update", $params); // FILTER HOOK
							$career = $params['career'];
							$where = $careers_table->getAdapter()->quoteInto("id = ?", $career['id']);
							if ($careers_table->update($career, $where) >= 1) {
								$this->view->success = "Career successfully updated.";
								$this->view->career = null;
							} else {
								$errors[] = "Unable to update the career due to a database error.";
							}
						} else {
							// INSERT NEW
							$career['id'] = null;
							$career['created_on'] = date(DB_DATETIME_FORMAT);
							$params['career'] = $career;
							$params = $this->_cts_plugin->doFilter("careers_admin_edit_pre_insert", $params); // FILTER HOOK
							$career = $params['career'];
							$career_id = $careers_table->insert($career);
							if ($career_id >= 1) {
								$this->view->success = "Career successfully added.";
								$this->view->career = null;
								$this->view->career_id = $career_id;
							} else {
								$errors[] = "Unable to add the career due to a database error.";
							}
						}
					}
				}
			} else {
				$errors[] = "Career not found.";
			}
			$locations_table = new CareersLocations();
			$this->view->locations = $locations_table->getOptions('id', 'name', 'Choose a location...');
		}
		$this->view->errors = $errors;
	}

	/*
		Function: bulkdelete
	*/
	function bulkdeleteAction() {
		$request = new Cts_Request($this->getRequest());
		$errors = array();
		if ($request->has('bulk_checkbox')) {
			$career_ids = $request->bulk_checkbox;
			$careers_table = new CareersCareers();
			if ($this->getRequest()->isPost()) {
				if ($careers_table->deleteBulk($career_ids)) {
					$this->view->success = "Careers deleted.";
				} else {
					$errors[] = "Unable to delete the careers due to a database error.";
				}
			} else {
				$careers = $careers_table->getBulk($career_ids);
				if (count($careers) >= 1) {
					$this->view->careers = $careers;
				} else {
					$errors[] = "No careers found.";
				}
			}
		} else {
			$errors[] = "No careers were selected.";
		}
	}

}
